<?php
	class Login extends Application{
		function __construct(){
			$this->loadModel('model_login');
		}
		
		function index(){
            if(isset($_SESSION['loginadmin']) && ($_SESSION['loginadmin'] == 1)){
                $this->loadView(TEMPLATE_ADMIN.'/index_body');
            }else{
                $this->loadView(TEMPLATE_ADMIN.'/login');
            }
		}
		function check(){
            if(isset($_POST['btnlogin'])){
                $username  = isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '';
                $password  = isset($_POST['password']) ? $_POST['password'] : '';  
                if($this->model_login->login($username, $password)){
                    $_SESSION['loginadmin'] = 1;
				    $_SESSION['useradmin'] = $username;
                    header("location: ?model=welcome");
                }else{
                    $data['error'] = "<script language=\"javascript\">alert(\"Đăng nhập không thành công!\");</script>";
                    $this->loadView(TEMPLATE_ADMIN.'/login', $data);
                }
            }
		}
        function logout(){
            unset($_SESSION['loginadmin']);
            unset($_SESSION['useradmin']);
    		header("Location: ".$_SERVER['HTTP_REFERER']);
        }
	}
?>